<?php
/*
 * Title: index.php
 * Description: Main page for proofGallery - 
 *				This is where all the magick comes together!
 * Package: proofGallery ver. 0.2
 * Author: Jesse Grobstein
 * Organization: I Services, Inc.
 * URI: http://iservicesinc.net
*/
require_once('functions.php');
if(!file_exists("config.php")) {
	if(!@$_POST['install']) {
		printHead();
		if(!fopen('config.php', 'w')) {
			$cfg = dirname(__FILE__);
			echo "<p><b>IMPORTANT!</b> This script requires write permissions to the following directories:<br/>
			<i>{$cfg}</i><br/>
			<i>{$cfg}/albums</i><br/>
			<br/>Please make sure that at least '<i>{$cfg}/albums</i>' is<br/>
			set to 0777 permissions for this script to function correctly.<br/>
			You can manually create your 'config.php' file in '<i>{$cfg}</i>'<br/>
			if we fail to do so for you.</p>";
		}
		echo "<h2>Install proofGallery</h2>
			<form action='' method='post'>
			<input type='text' name='key' placeholder='Private Key'/><br/>
			<input type='text' name='email' placeholder='Email Address'/><br/>
			<input type='submit' name='install' value='Install'/><br/>
			</form>
			<p><b>NOTE:</b> Please make sure you keep your 'Private Key' recorded in a safe place.<br/>You will need it to access the backend of your proofGallery installation.<br/>The email address you enter here will be used to send you approval notifications.</p>";
		printFoot();
	} else {
		$admin = $_POST['key'];
		$email = $_POST['email'];
		$config = "<?php\n\$admin = '{$admin}';\n\$email = '{$email}';\n?>";
		$htmlconfig = htmlspecialchars(str_replace('\n','\r', $config), ENT_QUOTES);
		$fh = fopen('config.php', 'w');
		if(!fwrite($fh, $config)) {
			$cfg = dirname(__FILE__);
			printHead();
			echo "<h4>Failed to write 'config.php' file</h4>
			<p>Please create a 'config.php' file in '{$cfg}' and paste the following line of code in it:</p>
			<label>config.php</label><br/>
			<textarea style='clear:both;margin:0 auto;height:80px;width:400px;'>{$htmlconfig}</textarea>";
			printFoot();
			exit();			
		} else {
			fclose($fh);
		}
		header("Location: ?admin={$admin}");
	}
	exit();	
}
if(isset($_GET['admin']) && $_GET['admin'] == $admin) {
	printHead();
	// Form to upload album and generate link
	echo "<h2>Existing Albums</h2>";
	admin_init();
	echo "<div class='clear'><br/></div>";
	if(@$message) echo "<p>$message</p>";
	print $uploadForm;
	printFoot();
	exit();
} else {
	printHead();
	?>
	<!-- The gallery container: -->
	<div id="gallery">
	<?php
	gallery_init();
	if($dh = @opendir($thumb_directory)) {
		$i=1;
		$files = array(); 
		while (false !== ($file = readdir($dh))) { 
			$flag = false; 
			if($file !== '.' && $file !== '..') $files[] = $file;
		}
		sort($files);
		echo '<form action="" method="post">';
		foreach ($files as $file) {
			if($file=='.' || $file == '..') continue;
			$file_parts = explode('.',$file);
			$ext = strtolower(array_pop($file_parts));
			$title = implode('.',$file_parts);
			$title = htmlspecialchars($title);
			if(in_array($ext,$allowed_types))
			{
				echo '
				<div class="pic">
					<a class="fancybox" rel="group" href="'.$orig_directory.'/'.$file.'" target="_blank" style="background:url('.$thumb_directory.'/'.$file.') center no-repeat;">'.$title.'</a>
					<input type="checkbox" name="picid[]" value="'.$title.'" class="checkpic"/>
				</div>
				';
			}
		}
		echo '<div class="clear"></div>';
		echo '<input class="btn" type="button" value="Toggle Selected" id="selectAll"/>';
		echo '<input class="btn" type="submit" value="Approve Selected" name="approvePhotos"/>';
		echo '</form>';
		closedir($dh);
	} else {die("<h2>Sorry!</h2><h3>We can't find what you're looking for...</h3>");}
	?>
	<div class="clear"></div>
	</div>
	<?php
	printFoot();
}
?>